﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.Entities.Relations.DirectAssociations
{
    [Serializable]
    public class EntityLinkageOutput:ResultBase
    {
        #region props
        private int _SrcEntityCount;
        /// <summary></summary>
        public int SrcEntityCount
        {
            get
            {
                return this._SrcEntityCount;
            }
            set
            {
                this._SrcEntityCount = value;
            }
        }

        private int _LinkedRecordCount;
        /// <summary></summary>
        public int LinkedRecordCount
        {
            get
            {
                return this._LinkedRecordCount;
            }
            set
            {
                this._LinkedRecordCount = value;
            }
        }

        #endregion

        #region ctor
        public EntityLinkageOutput():base(Guid.Empty,Guid.Empty)
        {
            this._SrcEntityCount = 0;
            this._LinkedRecordCount = 0;
        }

        public EntityLinkageOutput(Guid componentID, Guid jobTicketID,
            int srcEntityCount, int linkageCount) 
            : base(componentID, jobTicketID)
        {
            this._SrcEntityCount = srcEntityCount;
            this._LinkedRecordCount = linkageCount;
        }
        #endregion

        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                EntityLinkageOutput output = new EntityLinkageOutput();
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);

                output.SrcEntityCount = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "SrcEntityCount", "0"));
                output.LinkedRecordCount = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "LinkedRecordCount", "0"));

                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SrcEntityCount", this.SrcEntityCount.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "LinkedRecordCount", this.LinkedRecordCount.ToString());

            return xNode;
        }
    }
}
